function Orkut() {
    
    this.CONSUMER_SECRET = 'www.dxs.com.br';
    this.TOKEN_SECRET = '5tB823QvYMnbcb7IJpqhU+MX';
    this.REQUEST_TOKEN = 'https://www.google.com/accounts/OAuthGetRequestToken';
    this.ACCESS_TOKEN = 'https://www.google.com/accounts/OAuthGetAccessToken';
    this.SCOPE = 'http://orkut.gmodules.com/social/';
    this.RPC_ENDPOINT = 'http://www.orkut.com/social/rpc';

    this._initMessage = function(action, method) {
        
        var message = { action: action
                      , method: method
                      , parameters: []
                  };
        
        message.parameters.push(["oauth_version","1.0"]);
        message.parameters.push(['oauth_consumer_key', this.CONSUMER_SECRET]);
        message.parameters.push(['oauth_timestamp', OAuth.timestamp()]);
        message.parameters.push(['oauth_nonce', OAuth.nonce(32)]);
        message.parameters.push(["oauth_signature_method","HMAC-SHA1"]);
        
        return message;
        
    }
    
    this._sign = function( token, tokenSecret, message ) {
        
        token = OAuth.percentEncode(token);
        
        // join
        if(tokenSecret == '')
            token+='&';
        else
            token+='&' + OAuth.percentEncode(tokenSecret);
        
        var msg = OAuth.SignatureMethod.getBaseString(message).replace(/%2525/g,'%25');
            
        return OAuth.percentEncode(b64_hmac_sha1(token, msg)) + '=';    
    }
    
    this._sendRequest = function(url, realm, data, method, callback) {
    
        if(method=='POST')
            dataType='json';
        else
            dataType='text';
    
        $.ajax({
            url : url,
            async: false,
            data: data,
            type : method,
            dataType: dataType,
            contentType: "application/json",
            beforeSend : function(req) {
                if(realm!='')
                    req.setRequestHeader('Authorization', realm);
            },
            success: function(data) {
                callback(data);
            },
            
            error: function(data) {
                console.log('Error! ' + data);
            }
    
        });
    }
    
    this.requestToken = function() {
    
        var message = this._initMessage( this.REQUEST_TOKEN, 'GET' );
        
        // add specific parameters
        message.parameters.push(['scope', this.SCOPE]);
        message.parameters.push(['oauth_callback', 'http://localhost/callback.php']);

        // sign the request
        var sign = this._sign( this.TOKEN_SECRET, '', message)
        
        // get url parameters
        var norm = OAuth.SignatureMethod.normalizeParameters(message.parameters);
        
        // build the url
        var url = message.action + '?' + norm + '&oauth_signature=' + sign;
        
        
        this._sendRequest(url, '', '', 'GET', function(data){
            
            var keys = OAuth.getParameterMap(OAuth.decodeForm(data));
        
            // save.
            localStorage['oauth_token'] = keys['oauth_token'];
            localStorage['oauth_token_secret'] = keys['oauth_token_secret'];
            
            chrome.tabs.create({url: 'https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=' + keys['oauth_token']});
            
        });
    
    }
    
    this.upgradeRequestToken = function (oauth_verifier) {
        
        // create message
        var message = this._initMessage( this.ACCESS_TOKEN, 'GET' );
        message.parameters.push(['oauth_token', localStorage['oauth_token']]);
        message.parameters.push(['oauth_verifier', oauth_verifier]);
        
        // sign
        var sign = this._sign ( this.TOKEN_SECRET, localStorage['oauth_token_secret'] , message );
        
        // normalize - bug on the library, doubling % chars
        var norm = OAuth.SignatureMethod.normalizeParameters(message.parameters).replace(/%2525/g,'%25').replace(/%25/g,'%');
        
        // pack url
        var url = message.action + '?' + norm + '&oauth_signature=' + sign;
        
        this._sendRequest( url, '', '', 'GET', function(data) {
            var keys = OAuth.getParameterMap(OAuth.decodeForm(data));
            
            localStorage['oauth_token'] = keys['oauth_token'];
            localStorage['oauth_token_secret'] = keys['oauth_token_secret'];
            localStorage['authenticated'] = '1';
        });
        
    
    }
    
    this._sendRpc = function(rpc) {
    
         // create message
        var message = this._initMessage( this.RPC_ENDPOINT, 'POST' );
        message.parameters.push(['oauth_token', localStorage['oauth_token']]);
        message.parameters.push(['oauth_body_hash', b64_sha1( rpc )+'=']);
        
        // sign
        var sign = this._sign ( this.TOKEN_SECRET, localStorage['oauth_token_secret'] , message );
        message.parameters.push(['oauth_signature', sign]);
        
        var realm = OAuth.getAuthorizationHeader("", message.parameters).replace(/%25/g,'%');
        
        var ret = {}
        
        // send request, and return callback
        this._sendRequest( message.action, realm, rpc, 'POST', function(data) {
            ret = data;
        });
        
        return ret;
    }
    
    this.listFriends = function() {
        
        var rpc = '[{"method":"people.get","params":{"userId":["@me"],"groupId":"@friends","fields":["displayName","currentLocation","thumbnailUrl","gender","name","locality","lookingFor","relationshipStatus","sexualOrientation"],"count":300},"id":"friends"}]';
        return this._sendRpc(rpc);        
        
    }
    
    this.listScraps = function(uid, lastKey) {
        
        if(uid=='')
            uid='@me';
            
        var rpc = '';
        
        // pagination.
        if(lastKey=='')
            rpc = '[{"method":"messages.get","params":{"userId":["'+uid+'"],"groupId":"@friends","pageType":"first","messageType":"public_message","count": "50"},"id":"scraps"}]';
        else
            rpc = '[{"method":"messages.get","params":{"userId":["'+uid+'"],"groupId":"@friends","pageType":"prev","messageType":"public_message","count": "50","lastKey":"'+lastKey+'"},"id":"scraps"}]';
        
        return this._sendRpc(rpc);
    }
    
    this.sendScrap = function(uid, msg) {
    
        var now = new Date();
        var timeStamp = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
        var rpc = '[{"method":"messages.create","params":{"userId":["'+uid+'"],"groupId":"@self","message":{"recipients":[1],"body":"'+msg+'","title":"sent at '+timeStamp+'"},"messageType":"public_message"},"id":"send_scrap"}]';
        
        return this._sendRpc( rpc );
         
    }
    
    this.replyScrap = function(uid, msg, scrapId) {
    
        var now = new Date();
        var timeStamp = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
        var rpc = '[{"method":"messages.create","params":{"userId":["'+uid+'"],"groupId":"@self","message":{"recipients":[1],"body":"'+msg+'","title":"sent at '+timeStamp+'"},"messageType":"public_message","msgId":"'+scrapId+'"},"id":"send_scrap"}]';
        
        return this._sendRpc( rpc );
         
    }

}
